package com.schoolgard.action.alarm;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;

import com.schoolegard.services.AlarmClassService;
import com.schoolegard.services.AlarmHandleService;
import com.schoolegard.services.RoleService;
import com.schoolgard.base.BaseAction;
import com.schoolgard.entitybean.AlarmClass;
import com.schoolgard.entitybean.AlarmHandle;
import com.schoolgard.entitybean.GridDataModel;
import com.schoolgard.entitybean.Role;
import com.schoolgard.entitybean.TreeDataModel;
import com.supersystem.utils.JSONUtils;

public class AlarmHandleAction extends BaseAction{
	
	private AlarmHandleService alarmHandleService;
	
	private AlarmClassService alarmClassService;
	
	private RoleService roleService;
	
	private AlarmHandle alarmHandle;
	
	public void findAll(){
		String startStr = request.getParameter("start"); //grid传过来的当前数据条数
		 String limitStr = request.getParameter("limit"); //每页显示的数据数
		 
		 int start = Integer.parseInt(startStr);
	     int limit = Integer.parseInt(limitStr);
	     if (limit == 0) {
	    	 limit = Integer.MAX_VALUE;
	     }
	     int end = start + limit;  //当前页需要显示的数据 start--> end
	     List<AlarmHandle> handle = alarmHandleService.findAll(0,Integer.MAX_VALUE);
		int total = handle.size();
		if(end > total)end = total;
		
		GridDataModel<AlarmHandle> page = new GridDataModel<AlarmHandle>();
		page.setPage(start/limit + 1);
		page.setRows(handle);
		page.setTotal(total);
		String jsonStr = JSONObject.fromObject(page).toString();
		writeToResponse(jsonStr);
	}
	
	public void save(){
		Long id = alarmHandle.getId();
		AlarmHandle handle = null;
		if(id == null || id == 0) {
			handle = new AlarmHandle(); //新增
		} else {
			handle = alarmHandleService.findById(id);
		}
		handle.setAcceptor(alarmHandle.getAcceptor());
		handle.setAlarmClassId(alarmHandle.getAlarmClassId());
		handle.setIntervalValue(alarmHandle.getIntervalValue());
		handle.setName(alarmHandle.getName());
		handle.setRecvValue(alarmHandle.getRecvValue());
		handle.setType(alarmHandle.getType());
		handle.setAcceptorName(alarmHandle.getAcceptorName());
		handle.setAlarmClassName(alarmHandle.getAlarmClassName());
		alarmHandleService.saveOrUpdate(handle);
	}

	public void delete(){
		String id = request.getParameter("deleteId");
		if(StringUtils.isBlank(id)){
			writeToResponse("0");
			return ;
		}
		alarmHandleService.delete(Long.valueOf(id));
		writeToResponse("1");
	}
	
	public void findAlarmClassList(){
		List<TreeDataModel> orgTrees =  queryAlarmClassTree();
		writeToResponse(com.supersystem.utils.JSONUtils.toJSONString(orgTrees));
	}
	
	public void findRoleList(){
		List<TreeDataModel> orgTrees =  queryRoleTree();
		writeToResponse(com.supersystem.utils.JSONUtils.toJSONString(orgTrees));
	}
	
	public List<TreeDataModel> queryRoleTree(){
		List<TreeDataModel> treeNodes = new ArrayList<TreeDataModel>();
		List<Role> roles = roleService.findAllRole();
		for (Role role : roles) {
			TreeDataModel roleTree = new TreeDataModel();
			roleTree.setId(Long.toString(role.getId()));
			roleTree.setText(role.getName());
			treeNodes.add(roleTree);
		}
		return treeNodes;
	}
	
	public List<TreeDataModel> queryAlarmClassTree(){
		List<TreeDataModel> treeNodes = new ArrayList<TreeDataModel>();
		List<AlarmClass> alarmClasses = alarmClassService.findAll();
		TreeDataModel alarmTree_s = new TreeDataModel();
		alarmTree_s.setId("001");
		alarmTree_s.setText("软件告警");
		TreeDataModel alarmTree_h = new TreeDataModel();
		alarmTree_h.setText("002");
		alarmTree_h.setText("硬件告警");
		TreeDataModel alarmTree_n = new TreeDataModel();
		alarmTree_n.setText("003");
		alarmTree_n.setText("网络告警");
		List<TreeDataModel>  list_s = new ArrayList<TreeDataModel>();
		List<TreeDataModel>  list_h = new ArrayList<TreeDataModel>();
		List<TreeDataModel>  list_n = new ArrayList<TreeDataModel>();
		for (AlarmClass alarm : alarmClasses) {
			AlarmHandle ah = alarmHandleService.findByClassId(alarm.getId());
			if(ah != null) {
				continue;
			}
			TreeDataModel alarmTree = new TreeDataModel();
			alarmTree.setId(Long.toString(alarm.getId()));
			alarmTree.setText(alarm.getFormat());
			if(alarm.getType() == 1) {
				list_s.add(alarmTree);
			}else if(alarm.getType() == 2){
				list_h.add(alarmTree);
			}else{
				list_n.add(alarmTree);
			}
		}
		alarmTree_s.setChildren(list_s);
		alarmTree_h.setChildren(list_h);
		alarmTree_n.setChildren(list_n);
		treeNodes.add(alarmTree_s);
		treeNodes.add(alarmTree_h);
		treeNodes.add(alarmTree_n);
		return treeNodes;
	}
	
	public void findByID(){
		String id = request.getParameter("alarmHandleId");
		if(StringUtils.isEmpty(id)) {
			return;
		}
		AlarmHandle handle = alarmHandleService.findById(Long.valueOf(id));
		writeToResponse(JSONUtils.toJSONString(handle));
	}
	
	public AlarmHandleService getAlarmHandleService() {
		return alarmHandleService;
	}

	public void setAlarmHandleService(AlarmHandleService alarmHandleService) {
		this.alarmHandleService = alarmHandleService;
	}

	public AlarmHandle getAlarmHandle() {
		return alarmHandle;
	}

	public void setAlarmHandle(AlarmHandle alarmHandle) {
		this.alarmHandle = alarmHandle;
	}

	public void setAlarmClassService(AlarmClassService alarmClassService) {
		this.alarmClassService = alarmClassService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

}
